﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MDIApplication.Classes;

namespace MDIApplication
{
    public partial class CheckIn : Form
    {
        public CheckIn()
        {
            InitializeComponent();
        }

        #region Form_Load

        DataView checkInDv = new DataView();
        private void CheckIn_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'checkInDataSet.Booking' table. You can move, or remove it, as needed.
            this.bookingTableAdapter.Fill(this.checkInDataSet.Booking);

            DataTable bookTab = new DataTable();
            SqlConnection connection = new SqlConnection(@"Data Source=LUCKYPX\LUCKYPX;Initial Catalog=SWENAssignment3;Persist Security Info=True;User ID=sa;Password=imsa");
            connection.Open();
            bookTab.Load(new SqlCommand("Select bk.Booking_ID as 'Booking ID',bk.Check_In_Date as 'Check In Date',bk.Check_Out_Date as 'Check Out Date',bk.Additional_Remarks as 'Additional Remarks',bk.Late_Checkout_Indication as 'Late Checkout Indication',rm.Room_Unit_No as 'Room Unit No',rm.Room_Level as 'Room Level' from Booking bk, Room rm where bk.Check_in_Date >= CONVERT(VARCHAR(10), Getdate(), 101) and bk.Room_Unit_No=rm.Room_Unit_No and rm.Room_Status='Reserved'", connection).ExecuteReader());
            dataGridView1.DataSource = checkInDv = bookTab.DefaultView;
            connection.Close();

            //dataGridView1 Propeties
            dataGridView1.RowHeadersVisible = false;
            dataGridView1.AllowUserToResizeRows = false;
            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            dataGridView1.MultiSelect = false;
            dataGridView1.AllowUserToAddRows = false;
            dataGridView1.Rows[0].Selected = false;
            dataGridView1.ClearSelection();

            int height = 0;
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                height += row.Height;
            }
            height += dataGridView1.ColumnHeadersHeight;

            int width = 0;
            foreach (DataGridViewColumn col in dataGridView1.Columns)
            {
                width += col.Width;
            }
            width += dataGridView1.RowHeadersWidth;

            dataGridView1.ClientSize = new Size(width + 2, height + 2);

            toolStripButton2.Enabled = false; 
        }
        #endregion Form_Load

        #region dataGridView
        int selectedRowIndex;
        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                this.toolStripButton2.ForeColor = Color.Black;
                this.toolStripButton2.Font = new Font(toolStripButton1.Font, FontStyle.Bold);
                this.toolStripButton2.Enabled = true;
                selectedRowIndex = e.RowIndex;
            }
        }
        #endregion dataGridView

        #region toolStrip
        NewBooking newBookingChild;
        AllBookingscs allBookingChild;
        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            CloseAllForms();
            if (allBookingChild == null)
            {
                allBookingChild = new AllBookingscs();
                allBookingChild.Location = new Point(0, 0);
                // Set the Parent Form of the Child window.
                allBookingChild.MdiParent = this.MdiParent;
                // Display the form.
                allBookingChild.Show();
                allBookingChild.Activate();
                if (newBookingChild == null)
                {
                    newBookingChild = new NewBooking();
                    newBookingChild.MdiParent = this.MdiParent;
                    newBookingChild.Location = new Point(100, 70);
                    // Display the form.
                    newBookingChild.Show();
                    newBookingChild.Activate();
                }
            }
                   
        }

        int roomUnitNo;
        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            if (selectedRowIndex >= 0)
            {
                DataGridViewRow row = this.dataGridView1.Rows[selectedRowIndex];
                BookingRoomAvailabilityDBManager db = new BookingRoomAvailabilityDBManager();
                roomUnitNo = Convert.ToInt32(row.Cells[5].Value);
                db.updateRoomStatus(roomUnitNo,"Occupied");
            }

            DataTable bookTab = new DataTable();
            SqlConnection connection = new SqlConnection(@"Data Source=LUCKYPX\LUCKYPX;Initial Catalog=SWENAssignment3;Persist Security Info=True;User ID=sa;Password=imsa");
            connection.Open();
            bookTab.Load(new SqlCommand("Select bk.Booking_ID as 'Booking ID',bk.Check_In_Date as 'Check In Date',bk.Check_Out_Date as 'Check Out Date',bk.Additional_Remarks as 'Additional Remarks',bk.Late_Checkout_Indication as 'Late Checkout Indication',rm.Room_Unit_No as 'Room Unit No',rm.Room_Level as 'Room Level' from Booking bk, Room rm where bk.Check_in_Date >= CONVERT(VARCHAR(10), Getdate(), 101) and bk.Room_Unit_No=rm.Room_Unit_No and rm.Room_Status='Reserved'", connection).ExecuteReader());
            dataGridView1.DataSource = checkInDv = bookTab.DefaultView;
            connection.Close();
        }

        CheckOut checkOutChild;
        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            CloseAllForms();
            if (checkOutChild == null)
            {
                checkOutChild = new CheckOut();
                checkOutChild.MdiParent = this.MdiParent;
                checkOutChild.Location = new Point(0, 0);
                checkOutChild.Show();
                checkOutChild.Activate();
            }
        }

        GuestManagement guestManagementChild;
        private void toolStripButton4_Click(object sender, EventArgs e)
        {
            CloseAllForms();
            if (guestManagementChild == null)
            {
                guestManagementChild = new GuestManagement();
                guestManagementChild.Location = new Point(0, 0);
                guestManagementChild.MdiParent = this.MdiParent;
                guestManagementChild.Show();
                guestManagementChild.Activate();
            }
        }
        #endregion toolStrip

        #region CloseAllForms
        private void CloseAllForms()
        {
            Form[] formToClose = null;
            int i = 1;
            foreach (Form form in Application.OpenForms)
            {
                if (form != this && form.IsMdiChild) //this is form1
                {
                    Array.Resize(ref formToClose, i);
                    formToClose[i - 1] = form;
                    i++;
                }
            }
            if (formToClose != null)
            {
                for (int j = 0; j < formToClose.Length; j++)
                {
                    formToClose[j].Close();
                }
                checkOutChild = null;
                allBookingChild = null;
                newBookingChild = null;
                guestManagementChild = null;
            }
        }
        #endregion CloseAllForms



 
    }
}
